package com.mmall.utils;

import com.mmall.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

@Slf4j
public class RedisPoolUtil {

    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.set(key, value);
        } catch (Exception e) {
            log.error("set key:{} value:{} error",key,value,e);
        }finally {
            jedis.close();
        }
        return result;
    }

    //超时时间，单位为秒
    public static String setEx(String key, String value,int exTime) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.setex(key, exTime,value);
        } catch (Exception e) {
            log.error("setEx key:{} value:{} ,exTime:{},error",key,value,exTime,e);
        }finally {
            jedis.close();
        }
        return result;
    }
    //重新设置key的超时时间，单位为秒
    public static Long expire(String key,int exTime) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {
            log.error("expire key:{},exTime:{} error",key,exTime,e);
        }finally {
            jedis.close();
        }
        return result;
    }

    public static String get(String key) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.get(key);
        } catch (Exception e) {
            log.error("get key:{} value:{} error",e);
        }finally {
            jedis.close();
        }
        return result;
    }
    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.del(key);
        } catch (Exception e) {
            log.error("del key:{}, error",key,e);
        }finally {
            jedis.close();
        }
        return result;
    }

    /**分布式锁使用
     * @param key
     * @param value
     * @return
     */
    public static Long setnx(String key, String value) {
        Jedis jedis = null;
        Long result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.setnx(key,value);
        } catch (Exception e) {
            log.error("setnx key:{} value:{} error",key,value,e);
        }finally {
            jedis.close();
        }
        return result;
    }

    //设置值，并返回旧的值
    public static String getSet(String key, String value) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getResource();
            result = jedis.getSet(key, value);
        } catch (Exception e) {
            log.error("getSet key:{} value:{} error",key,value,e);
        }finally {
            jedis.close();
        }
        return result;
    }

    public static void main(String[] args) {
        Jedis jedis = RedisPool.getResource();
        RedisPoolUtil.set("keyTest", "value");

        String value = RedisPoolUtil.get("keyTest");
        System.out.println("value:" + value);

        RedisPoolUtil.setEx("keyEx", "keyExValue", 60*10);

        RedisPoolUtil.expire("keyTest", 60 * 20);

        RedisPoolUtil.del("keyTest");

        System.out.println("end");

    }
}
